import axios from '@/utils/axios.js'
import {
	customApiBaseURL as baseURL,
	hkIp
} from '@/config/index.js';


/**
 * 获取平台级隐私申明
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const authPrivacyPolicyUrl = (data) => axios({
	method: 'get',
	header: {
		"isToken": false
	},
	data: data,
	url: baseURL + '/metaworks/iam/uc/v2/privacyPolicyUrl'
})

/**
 * 获取租户最新隐私声明
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const privacyAgreementLatest = (data) => axios({
	method: 'get',
	header: {
		"isToken": true,
		"tenant-code": false
	},
	data: data,
	url: baseURL + '/metaworks/iam/uc/v1/tenant/privacyAgreement/latest'
})

/**
 * 同意/撤销隐私声明
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const privacyAgreementSign = (data) => axios({
	method: 'post',
	header: {
		"isToken": true,
		"tenant-code": false
	},
	data: data,
	url: baseURL + '/metaworks/iam/uc/v1/tenant/user/privacyAgreement/sign'
})

/**
 * 登录接口
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const authLogin = (data) => axios({
	method: 'post',
	header: {
		"isToken": false
	},
	data: data,
	url: baseURL + '/metaworks/iam/app/uc/app/login'
})

/**
 * 获取用户权限接口
 * @param {String} [id] 用户id
 * @returns {promise}
 */
export const authDetails = (userId, data) => axios({
	method: 'get',
	data: data,
	url: baseURL + `/metaworks/iam/uc/v1/user_role/${userId}/permissions`
})

/**
 * 获取用户详情2
 * @param {String} [id] 用户id
 * @returns {promise}
 */
export const authDetails2 = (userId, data) => axios({
	method: 'get',
	data: data,
	url: baseURL + `/metaworks/iam//app/uc/app/users/${userId}/details`
})


/**
 * 获取机机token
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const xminerToken = (data) => axios({
	method: 'post',
	data: data,
	url: baseURL + `/apiaccess/api/xminerToken`,
})

/**
 * 获取组织结构
 * @param {String} [id] 组织id
 * @returns {promise}
 */
export const dept = (nodeId, token, data) => axios({
	method: 'get',
	data: data,
	header: {
		'gc-authentication': token
	},
	url: baseURL + `/metaworks/iam/uc/v1/base_nodes/by_parent?nodeId=${nodeId}`,
})

/**
 * 获取xMiner数据
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const getXminerData = (data) => axios({
	method: 'post',
	header: {
		"isToken": true
	},
	data: data,
	// url: baseURL + '/apiaccess/api/xMiner_N'
	url: baseURL + '/metaworks/dcp/data/data/v3/getByApiCode'
})

/**
 * 获取xMiner数据
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const getByApiCode = (data) => axios({
	method: 'post',
	header: {
		"isToken": true
	},
	data: data,
	url: baseURL + '/metaworks/quality/api/v1/dataConvert/getByApiCode'
})

/**
 * 获取apiFabric数据
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const getApiFabric = (data) => axios({
	method: 'post',
	header: {
		"isToken": true
	},
	data: data,
	url: baseURL + '/metaworks/api-manager/api/v1/flatten/exchange'
})

/**
 * 获取apiFabric数据
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const callApiBySuffix = ({
	suffix,
	data = ''
}) => axios({
	method: 'post',
	data: data,
	url: baseURL + '/apiaccess/api/' + suffix
})

/** 
 * APP公告列表
 */
export const appAnnouncement = (data) => axios({
	method: 'post',
	header: {
		"isToken": true
	},
	data: {
		"queryJson": "{\"columns\":[],\"dataSource\":{\"name\":\"244035094162010112\",\"type\":\"table\"},\"rowCount\":20,\"offset\":0}",
		"viewId": "244035094174593024"
	},
	url: baseURL + '/metaworks/eform-record/v1/data/query',
})

/** 
 * APP安装包列表
 */
export const appList = (data) => axios({
	method: 'post',
	header: {
		"isToken": true
	},
	data: {
		"queryJson": "{\"columns\":[],\"dataSource\":{\"name\":\"241442093329903616\",\"type\":\"table\"},\"rowCount\":20,\"offset\":0}",
		"viewId": "241442093338292224"
	},
	url: baseURL + '/metaworks/eform-record/v1/data/query'
})

/** 
 * 获取图片
 */
export const img = (data) => axios({
	method: 'get',
	header: {
		isToken: true,
		responseType: 'arraybuffer',
	},
	url: baseURL + `/metaworks/eform-record/v1/file/download/241364805133365248/242089161534828544/${data}`
})

/**
 * cid
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const setCid = (data) => axios({
	method: 'post',
	data: data,
	url: 'http://192.168.3.83:8888/push/pushMess/single'
})

/**
 * 签署平台级隐私申明
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const authPrivacyAgreement = (data) => axios({
	method: 'post',
	header: {
		"isToken": true
	},
	data: data,
	url: baseURL + '/metaworks/iam/uc/v1/privacyAgreement'
})
/**
 * 签署平台级隐私申明
 * @param {object} [data] 发送键值对数据
 * @returns {promise}
 */
export const authPrivacyAgreement1 = (data) => axios({
	method: 'get',
	header: {
		"isToken": true
	},
	data: data,
	url: baseURL + '/metaworks/iam/uc/v1/tenant/privacyAgreement/lastet'
})


/**
 * 海康签名
 */
import CryptoJS from 'crypto-js'
const sign = (key, value) => {
	return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(value, key))
}
const appKey = '25975628' // 访问密钥ID
const appSecret = '016yTixcjLKV0j5TpSbN' // 与访问密钥ID结合使用的密钥，对请求进行加密签名
// 获取海康请求头
function getHeaders(url) {
	let sign_str = `POST\n*/*\napplication/json\nx-ca-key:${appKey}\n${url}`
	let signature = sign(appSecret, sign_str)
	let header = {
		'Accept': '*/*',
		'Content-Type': 'application/json',
		'x-ca-key': appKey,
		'x-ca-signature': signature,
		'x-ca-signature-headers': 'x-ca-key',
		"isToken": false
	}
	return header
}

let hkiRegionsApi = '/artemis/api/resource/v1/cameras',
	hkiSubRegionsApi = '/artemis/api/resource/v1/regions/subRegions',
	hkiUrlsApi = '/artemis/api/video/v2/cameras/previewURLs',
	hkiNameApi = '/artemis/api/resource/v1/cameras/indexCode'

/** 
 * 获取监控区域数据
 */
export const getRegions = (data) => axios({
	method: 'post',
	header: getHeaders(hkiRegionsApi),
	data: {
		"pageNo": 1,
		"pageSize": 200
	},
	url: baseURL + hkiRegionsApi
})
/** 
 * 获取下一级监控点数据
 */
export const getSubRegions = (data) => axios({
	method: 'post',
	header: getHeaders(hkiSubRegionsApi),
	data: {
		"parentIndexCode": "root000000",
		"resourceType": "camera",
		"pageNo": 1,
		"pageSize": 100,
		"cascadeFlag": 0
	},
	url: baseURL + hkiSubRegionsApi
})
/** 
 * 获取播放url数据
 */
export const getURLS = (data) => axios({
	method: 'post',
	header: getHeaders(hkiUrlsApi),
	data: data,
	url: hkIp + hkiUrlsApi
})

/** 
 * 获取摄像头点位的名称
 */
export const getName = (data) => axios({
	method: 'post',
	header: getHeaders(hkiNameApi),
	data: data,
	url: hkIp + hkiNameApi
})

/** 
 * app最新版本信息
 */
export const updateApp = () => axios({
	method: 'get',
	header: {
		"isToken": false
	},
	url: "https://api.appmeta.cn/apps/latest/664f194123389f14edc136c5/?api_token=95b1079318db09189e560db8504baff0"
})